<?php
/**
 * 提现控制器
 */
namespace Admin\Controller;
use Think\Controller;
class WithdrawalController extends InitController {
    
	// 提现规则
	public function Withdrawal_rule() {
		
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$opt     = M('Options')->where('option_name = "withdrawal_rule"')->find();
		
		$this->assign('opt',$opt);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);

		$this->display('Withdrawal/withdrawal_rule');
	}
	
	//保存提现规则
	public function Withdrawal_rule_save(){
		if(!IS_AJAX){
			return false;
		}
		$withdrawal_money = I('withdrawal_money',0.00,'floatval,trim');
		if(empty($withdrawal_money)){
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'提现规则金额不能为0','field'=>''));
		}
		
		$result_options = M('Options')->where('option_name = "withdrawal_rule"')->find();
		if($result_options){
			//保存提现金额
			$result = M('Options')->where('option_name = "withdrawal_rule"')->save(array('option_value'=>$withdrawal_money));
		}else{
			$result = M('Options')->add(array('option_name'=>'withdrawal_rule','option_value'=>$withdrawal_money));
		}
		if($result !== false){
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'保存成功','field'=>''));
		}else{
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'保存失败','field'=>''));
		}
	}
	
	//提现记录
	public function Withdrawal_list(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$where = '';
		
		$name = I('name');
		if($name){
			$where['name'] = array('LIKE','%'.$name.'%');
			$this->assign('name',$name);
		}
		$paytype = I('paytype');
		if($paytype){
			$where['paytype'] = $paytype;
			$this->assign('paytype',$paytype);
		}
		
		$status = I('status');
		if($status !== ''){
			$where['status'] = $status;
			$this->assign('status',$status);
		}
		
		$start_time = I('get.start_time','','strip_tags,htmlspecialchars,trim');
		$end_time = I('get.end_time','','strip_tags,htmlspecialchars,trim');
		
		if ($start_time !== '') {
			$start_time = strtotime($start_time);
			$this->assign('start_time',$start_time);
		}
		
		if ($end_time !== '') {
			$end_time = strtotime($end_time);
			$this->assign('end_time',$end_time);
		}
		
		if (!empty($start_time) && !empty($end_time)) {
			// 如果有开始及结束时间 则取中值
			$where['createtime'] = array(array('EGT', $start_time), array('ELT', $end_time));
		}else{
			if (!empty($start_time)) {
				// 有开始时间
				$where['createtime'] = array('EGT', $start_time);
			}elseif (!empty($end_time)) {
				// 有结束时间
				$where['createtime'] = array('ELT', $end_time);
			}
		}
		
		$withdrawalModel = D('WithdrawalApply');
		$total = $withdrawalModel->getWithdrawalNum($where);
		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';
		$list = $withdrawalModel->withdrawal_list($where,$Page->firstRow.','.$Page->listRows);
		//查询会员帐号
		if(!empty($list)){
			foreach($list as $k=>$v){
				$username = M('User')->where('id = %d',$v['uid'])->field('username,mobile')->find();
				$list[$k]['username'] = $username['username'];
				$list[$k]['mobile']   = $username['mobile'];
			}
		}
		
		
		$this->assign('list',$list);
		$this->assign('pager',$show);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		
		$this->display('Withdrawal/withdrawal_list');
	}
	
	//审核提现
	public function Withdrawal_Edit(){
		$id = I('id',0,'intval');
		if (!$id) {
			$this->error('参数错误');
		}

		$info = M('WithdrawalApply')->where(array('id'=>$id))->find();

		if(empty($info)){
			$this->error('没有找到应用提现申请记录');
		}
		
		$bank_info = M('BankInfo')->where(array('uid'=>$info['uid']))->find();

		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		//审核通过
		if(IS_AJAX){
			$status = I('status',0,'intval');
			
			if($status != 1){
				$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误'));
			}
			
			if($info['status'] != 0 ){			
				$this->ajaxReturn(array('error'=>1,'errmsg'=>'保存失败'));
			}
			
			$rc = M('WithdrawalApply')->where(array('id'=>$id))->save(array('status'=>'1'));
			
			if($rc !== false){
				$type = 3;

				information_content($info['uid'],$type,$info);

				$this->ajaxReturn(array('error'=>0,'errmsg'=>'保存成功','url'=>U('Withdrawal/Withdrawal_list',array('k'=>'7'))));
			}else{
				$this->ajaxReturn(array('error'=>1,'errmsg'=>'保存失败'));
			}		
		}
		
		$this->assign('info',$info);
		$this->assign('bank_info',$bank_info);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('withdrawal_edit');
	}
	
	//提现作废
	public function Withdrawal_Invalid(){
		$id = I('id',0,'intval');
		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误'));
		}
		
		if(!empty($id)){
			$result = M('WithdrawalApply')->where(array("id"=>$id))->save(array('status' => '-1'));
		}
		
		if($result != false){
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'保存成功','url'=>U('Withdrawal/Withdrawal_list',array('k'=>'7'))));
		}else{
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'保存失败'));
		}
		
	}
	


}